package firstping.controller;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import firstping.data.QnAPersistenceManager;
import firstping.model.Customer;
import firstping.model.Domain;

@SuppressWarnings("serial")
public class AccountServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		HttpSession session = req.getSession();
		String customer = (String) session.getAttribute("customer");
		
		if(customer != null)
		{
			Query query = psm.newQuery(Domain.class);
			query.setFilter("email=='"+customer+"'");
			@SuppressWarnings("unchecked")
			List<Domain> domains = (List<Domain>)query.execute();
			req.setAttribute("domains", domains);
			
			req.setAttribute("title", "Management infomation account");
			req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site");
			req.setAttribute("description", "Management infomation account.We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
			
			try {
				req.getRequestDispatcher("/account.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			resp.sendRedirect("/");
		}	
	}
}
